package com.lx.base.util;

import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lixing
 * @date 2022/8/9 14:00
 */
public class ListUtil {
    private static int defaultSiz=100;
    public static  <T>List<List<T>> splitList(List<T> list, int size){
        //判断是否为空
        if(CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        //计算分割后大小
        int maxSize=(list.size()+size-1)/size;
        return Stream.iterate(0,n->n+1).limit(maxSize)
                .parallel()
                .map(a->list.parallelStream().skip(a*size).limit(size).collect(Collectors.toList()))
                .filter(b->!b.isEmpty())
                .collect(Collectors.toList());
    }

    public static  <T>List<List<T>> splitList(List<T> list){
       return splitList(list,defaultSiz);
    }
}
